home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-01-07 | 6.3 KB | 255 lines | [TEXT/PJMM] |
- UNIT MyFileStuff;
-
- INTERFACE
-
- USES
- ROM85, PrintTraps, PlotGlobals, Misc;
-
- PROCEDURE doSaveAs;
- PROCEDURE doSave;
-
- IMPLEMENTATION
-
- PROCEDURE doSaveAs;
- LABEL
- 1, 2;
- CONST
- SFPutLeft = 82;
- SFPutTop = 50;
- headerBytes = 512;
- TYPE
- DrawHeader = RECORD
- fill : ARRAY[1..256] OF integer;
- END;
- VAR
- SFPutPt : Point;
- theReply : SFReply;
- err : OSErr;
- refNum : Integer;
- bytes : LongInt;
- myWindow : WindowPtr;
- title : str255;
- myType : OSType;
- myCreator : OSType;
- str1, str2 : str255;
- header : DrawHeader;
- i : integer;
- myPicture : PicHandle;
- PicLength : LongInt;
- BEGIN
- myPicture := PlotDocHandle^^.Drawing;
- IF myPicture = NIL THEN
- doMessage('No picture to save yet!', '', '', '')
- ELSE
- BEGIN
- SetPt(SFPutPt, SFPutLeft, SFPutTop);
- WITH header DO
- BEGIN
- FOR i := 1 TO headerBytes DIV 2 DO {512 bytes}
- fill[i] := 0;
- END;
- bytes := headerBytes;
- myWindow := PlotWindow;
- GetWTitle(myWindow, title);
- SFPutFile(SFPutPt, 'Save Plot as…', title, NIL, theReply);
- IF theReply.good THEN
- BEGIN
- myType := 'PICT';
- myCreator := 'MDRW'; {MacDraw doc}
- err := Create(theReply.fname, theReply.vRefNum, myCreator, myType);
- IF err <> noErr THEN
- BEGIN
- IF err = dupFNErr THEN
- BEGIN
- err := FSDelete(theReply.fname, theReply.vRefNum);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot delete duplicate file.', '', '', '');
- GOTO 1;
- END;
- err := Create(theReply.fname, theReply.vRefNum, myCreator, myType);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot create file...', '', '', '');
- GOTO 1;
- END;
- END
- ELSE
- BEGIN
- doMessage('Cannot create new file...', '', '', '');
- GOTO 1;
- END;
- END;
- err := FSOpen(theReply.fname, theReply.vRefNum, refNum);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot open file...', '', '', '');
- GOTO 1;
- END;
- err := SetFPos(refNum, FSFromStart, 0);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot set start of file...', '', '', '');
- GOTO 2;
- END;
-
- err := FSWrite(refNum, bytes, @header);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot write header to file...', '', '', '');
- GOTO 2;
- END;
- IF bytes <> 512 THEN
- BEGIN
- NumToString(bytes, str1);
- str1 := concat(str1, ' bytes');
- str2 := concat('out of ', '512');
- doMessage('Only able to write ', str1, str2, 'to file.');
- GOTO 2;
- END;
- PicLength := GetHandleSize(Handle(DrawingPic));
- bytes := PicLength;
- err := FSWrite(refNum, bytes, pointer(DrawingPic^));
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot write picture to file...', '', '', '');
- GOTO 2;
- END;
- IF bytes <> PicLength THEN
- BEGIN
- NumToString(bytes, str1);
- str1 := concat(str1, ' bytes');
- NumToString(PicLength, str2);
- str2 := concat('out of ', str2);
- doMessage('Only able to write ', str1, str2, 'to file.');
- GOTO 2;
- END;
- SetWTitle(myWindow, theReply.fname);
- PlotDocHandle^^.FileName := theReply.fname;
- PlotDocHandle^^.VolRefNum := theReply.vRefNum;
- EnableItem(myMenus[FileM], fSave);
- 2 :
- err := FSClose(refNum);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot close file...', '', '', '');
- ExitToShell;
- END;
- err := FlushVol(NIL, theReply.vRefNum);
- IF err <> NoErr THEN
- BEGIN
- doMessage('Cannot flush volume...', '', '', '');
- ExitToShell;
- END;
- 1 :
- SetCursor(arrow);
- END; {if good}
- END; {else pic exits}
- END;{ of proc}
-
- PROCEDURE doSave;
- LABEL
- 1, 2;
- CONST
- headerBytes = 512;
- TYPE
- DrawHeader = RECORD
- fill : ARRAY[1..256] OF integer;
- END;
- VAR
- err : OSErr;
- refNum : Integer;
- bytes : LongInt;
- myWindow : WindowPtr;
- title : str255;
- str1, str2 : str255;
- header : DrawHeader;
- i : integer;
- myPicture : PicHandle;
- PicLength : LongInt;
- myRefNum : integer;
- myFname : str255;
- BEGIN
- myPicture := PlotDocHandle^^.Drawing;
- IF myPicture = NIL THEN
- doMessage('No picture to save yet!', '', '', '')
- ELSE
- BEGIN
- myRefNum := PlotDocHandle^^.VolRefNum;
- myFname := PlotDocHandle^^.FileName;
- IF myRefNum = 0 THEN
- BEGIN
- doMessage('Cannot save file', 'Use SaveAs...', '', '');
- GOTO 1;
- END;
-
- WITH header DO
- BEGIN
- FOR i := 1 TO headerBytes DIV 2 DO {512 bytes}
- fill[i] := 0;
- END;
- bytes := headerBytes;
-
- err := FSOpen(myFname, myRefNum, refNum);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot open file...', '', '', '');
- GOTO 1;
- END;
- err := SetFPos(refNum, FSFromStart, 0);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot set start of file...', '', '', '');
- GOTO 2;
- END;
-
- err := FSWrite(refNum, bytes, @header);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot write header to file...', '', '', '');
- GOTO 2;
- END;
- IF bytes <> 512 THEN
- BEGIN
- NumToString(bytes, str1);
- str1 := concat(str1, ' bytes');
- str2 := concat('out of ', '512');
- doMessage('Only able to write ', str1, str2, 'to file.');
- GOTO 2;
- END;
- PicLength := GetHandleSize(Handle(DrawingPic));
- bytes := PicLength;
- err := FSWrite(refNum, bytes, pointer(DrawingPic^));
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot write picture to file...', '', '', '');
- GOTO 2;
- END;
- IF bytes <> PicLength THEN
- BEGIN
- NumToString(bytes, str1);
- str1 := concat(str1, ' bytes');
- NumToString(PicLength, str2);
- str2 := concat('out of ', str2);
- doMessage('Only able to write ', str1, str2, 'to file.');
- GOTO 2;
- END;
- 2 :
- err := FSClose(refNum);
- IF err <> noErr THEN
- BEGIN
- doMessage('Cannot close file...', '', '', '');
- ExitToShell;
- END;
- err := FlushVol(NIL, myRefNum);
- IF err <> NoErr THEN
- BEGIN
- doMessage('Cannot flush volume...', '', '', '');
- ExitToShell;
- END;
- 1 :
- SetCursor(arrow);
- END; {if good}
- END;{ of proc}
-
- END.